package com.truonghau.shipper.service;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;

public class MyGPSService extends Service {
	String GPS_FILTER = "guc.action.GPS_LOCATION";
	Thread triggerService;
	LocationManager lm;
	GPSListener myLocationListener;
	boolean isRunning = true;
	NotificationManager mNM;
	 
	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();

		triggerService = new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					Looper.prepare();
					lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
					myLocationListener = new GPSListener();
					long minTime = 10000;
					float minDistance = 50;
					
					lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime,
					minDistance, myLocationListener);
					Looper.loop();
				} catch (Exception e) {
					System.out.println("");
					// TODO: handle exception
				}
				
			}
		});
		triggerService.start();
	}

	private class GPSListener implements LocationListener{

		@Override
		public void onLocationChanged(Location location) {
			double lat = location.getLatitude();  // kinh do
			double lon = location.getLongitude();  // vi do
			Intent myFilterRes = new Intent(GPS_FILTER);
			myFilterRes.putExtra("lat", lat);
			myFilterRes.putExtra("lon", lon);
			Log.e(">>gps<<", lat +" : " +lon);
			sendBroadcast(myFilterRes);
//			showNotification(lat, lon);
		}

		@Override
		public void onProviderDisabled(String provider) {

		}

		@Override
		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub

		}

	}
}

